;;; config.el --- ctrl-better-defaults Layer Configuration File for Spacemacs
;;

(setq-default fill-column 80)

;; set default shell and get rid of PATH errors
;; (setq shell-file-name "/bin/bash")
;; (setq exec-path-from-shell-arguments '("-l"))

;; add auto format paste code
(dolist (command '(yank yank-pop))
  (eval
   `(defadvice ,command (after indent-region activate)
      (and (not current-prefix-arg)
           (member major-mode
                   '(emacs-lisp-mode
                     lisp-mode
                     clojure-mode
                     scheme-mode
                     haskell-mode
                     ruby-mode
                     rspec-mode
                     python-mode
                     c-mode
                     c++-mode
                     objc-mode
                     latex-mode
                     js-mode
                     plain-tex-mode))
           (let ((mark-even-if-inactive transient-mark-mode))
             (indent-region (region-beginning) (region-end) nil))))))

(setq large-file-warning-threshold 10000000)

;; skip a confirmation prompt for killing subprocesses when exiting emacs
(setq confirm-kill-processes nil)

;;Don't ask me when kill process buffer
(setq kill-buffer-query-functions
      (remq 'process-kill-buffer-query-function
            kill-buffer-query-functions))

(defun ctrl/cleanup-recentf ()
  "cleanup recent files"
  (progn
    (and (fboundp 'recentf-cleanup)
         (recentf-cleanup))))

;; cleanup recent files when emacs exit
(add-hook 'kill-emacs-hook #'ctrl/cleanup-recentf)

(defun spacemacs/check-large-file ()
  "turn off line number when open a large file"
  (when (> (buffer-size) 500000)
    (progn (fundamental-mode)
           (hl-line-mode -1)))
  (if (and (executable-find "wc")
           (> (string-to-number (shell-command-to-string (format "wc -l %s" (buffer-file-name))))
              5000))
      (display-line-numbers-mode -1)))

;; check file size is not too lage when open a file
(add-hook 'find-file-hook 'spacemacs/check-large-file)

;; abort the minibuffer when using the mouse
;; http://trey-jackson.blogspot.com/2010/04/emacs-tip-36-abort-minibuffer-when.html
(defun ctrl/stop-using-minibuffer ()
  "kill the minibuffer"
  (when (and (>= (recursion-depth) 1) (active-minibuffer-window))
    (abort-recursive-edit)))

;; abort the minibuffer when using the mouse
(add-hook 'mouse-leave-buffer-hook 'ctrl/stop-using-minibuffer)

;; add dired mode key bindings
(add-hook 'dired-load-hook (lambda()
                             ;; kill buffer not quit window when press q
                             (define-key dired-mode-map (kbd "q") #'kill-this-buffer)
                             ;; goto up directory and resue buffer
                             (define-key dired-mode-map (kbd "C-K") 'ctrl/dired-up-directory)
                             ;; get size of marked files
                             (define-key dired-mode-map (kbd "z") 'ctrl/dired-get-size)
                             ;; Open an `ansi-term' that corresponds to current directory."
                             (define-key dired-mode-map (kbd "`") 'ctrl/dired-open-term)
                             (define-key dired-mode-map (kbd ")") 'dired-omit-mode)
                             ;; copy file to visiting directory
                             (define-key dired-mode-map (kbd "c") 'ctrl/dired-copy-file-here)))

(setq backup-by-copying t
      make-backup-files nil
      create-lockfiles nil)


(spacemacs|use-package-add-hook ivy
  :post-config
  ;; hide . and .. directories
  (setq ivy-extra-directories nil)
  ;; ivy-occur is no longer editable in evil
  ;; https://github.com/syl20bnr/spacemacs/issues/10290
  ;; (evil-set-initial-state 'ivy-occur-grep-mode 'normal)
  ;; allow ivy use input word as prompt
  (setq ivy-use-selectable-prompt t)
  ;; ignore certain filename patterns in `find-file`/`counsel-find-file`
  (setq counsel-find-file-ignore-regexp
        (concat
         ;; File names beginning with # or .
         "\\(?:\\`[#.]\\)"
         ;; File names ending with # or ~
         "\\|\\(?:\\`.+?[#~]\\'\\)"
         "\\|\\.pyc\\'"))
  ;; hide some buffers, use ctrl-c ctrl-a to show it
  (add-to-list 'ivy-ignore-buffers "\\*spacemacs\\*")
  (add-to-list 'ivy-ignore-buffers "\\*Messages\\*")
  ;; get rid of the epc con buffers generated by EPC server when use importmagic
  ;; enable importmagic minor mode in python mode for now
  ;; https://github.com/anachronic/importmagic.el#buffers
  (add-to-list 'ivy-ignore-buffers "\\*epc con"))

;; exclude smartparens package use electric-part-mode
(electric-pair-mode t)
;; https://www.reddit.com/r/emacs/comments/4xhxfw/how_to_tune_the_behavior_of_eletricpairmode/
(setq electric-pair-inhibit-predicate 'electric-pair-conservative-inhibit)
(show-paren-mode t)

(add-hook 'before-save-hook 'delete-trailing-whitespace)
